#include <iostream>
#include <stack>

std::string removeDuplicates(std::string s) {
    std::stack<char> theStack;
    for (int i{}, len = s.size(); i < len; i++) {
        char c = s[i];
        if (theStack.empty()) {
            theStack.push(c);
        } else {
            if (theStack.top() == c) {
                theStack.pop();
            } else {
                theStack.push(c);
            }
        }
    }
    std::string result {};
    int size = theStack.size();
    result.resize(size);
    while (size--) {
        result[size] = theStack.top();
        theStack.pop();
    }

    return result;
}

int main() {
    std::string s { "abbaca" };
    std::string result = removeDuplicates(s);
    std::cout << "result: " << result << "\n";
    
    return 0;
}